package com.generic.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.HibernateException;

import com.generic.ds.HibernateSession;
import com.generic.handler.impl.SellerHandler;
import com.generic.module.impl.Seller;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

@InterceptorRef("jsonValidationWorkflowStack")
public class SignUpSeller extends ActionSupport {

	private static final long serialVersionUID = -6254907994323513932L;
	private String email = null;
	private String password = null;
	private String password2 = null;
	private String companyName = null;
	private String firstName = null;
	private String lastName = null;
	
	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getPassword2() {
		return password2;
	}

	public String getCompanyName() {
		return companyName;
	}
	
	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}
		
	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@EmailValidator(key = "error.invalid")
	public void setEmail(String email) {
		this.email = email;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "6",  maxLength = "20")
	public void setPassword(String password) {
		this.password = password;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@FieldExpressionValidator(key = "error.notequal", expression = "password eq password2" )
	public void setPassword2(String password) {
		this.password2 = password;
	}
	
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "2",  maxLength = "20")
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "2",  maxLength = "20")
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "2",  maxLength = "20")
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	@SkipValidation
	public String execute() {
		return SUCCESS;
	}
	
	@Action(value="/sign-up-seller-add",results={
			@Result(name="input",location="ajax-msg.jsp"),
			@Result(name="success",location="ajax-msg.jsp")})
	public String add() throws Exception {
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();
			
			SellerHandler dao = new SellerHandler(hs);
			Seller seller = (Seller)dao.get("email", email);
			if(seller == null) {
				dao.add(getSeller());
				hs.commitTransaction();
				addActionMessage(getText("success"));
			} else {
				addFieldError("email", getText("error.duplicate"));
			}			
		} catch (HibernateException e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}

		return SUCCESS;
	}
	
	private Seller getSeller() {
		Seller seller = new Seller();
		seller.setEmail(email);
		seller.setPassword(password);
		seller.setFirstName(firstName);
		seller.setLastName(lastName);
		seller.setCompanyName(companyName);
		return seller;
	}
	
}
